/**
 * 总控制器
 */
var index = avalon.define({
	$id : "indexController",
	dataList : {},
	title : '分类管理',
	limit : 20,
	name : "",
	pwd : "",
	sex : "",
	role : "",
	result : {},
	user : {},
	url : "welcome",
	isManager : false,
	titlename : "",
	isTcher : true,
	toggle : function(requseturl) {
		$.ajax({
			type : "POST",
			url : requseturl,
			data : {},
			success : function(result) {
				index.result = jQuery.parseJSON(result);
				if (index.result.success == true) {
					index.url = index.result.url;
					if (index.result.url == "TcherlistPage") {
						TcherManage.load();
					}

					if (index.result.url == "StulistPage") {
						StuManage.load();
					}

					if (index.result.url == "AppraiseTcher") {
						AppraisetcherManage.load();
					}

					if (index.result.url == "AppraiseList") {
						AppraiseList.load();
					}

					if (index.result.url == "AdminTcherlistPage") {
						adminsee.adminSee();
					}
					if (index.result.url == "ClassList") {
						AppraiseList.TcherSeeList();
					}

					if (index.result.url == "ScoreProj") {
						ScoreProj.load();
					}
				}
			}
		});
	},

	welcome : function() {
		index.url = "welcome";
	},

	load : function() {
		$.ajax({
			type : "POST",
			url : "login_getcurrentuser",
			data : {},
			success : function(result) {
				var data = jQuery.parseJSON(result);
				index.user = data.item;
				if (index.user.role.roleName == "管理员") {
					index.isManager = true;
					index.titlename = "管理员";
					index.isTcher = false;
				} else if (index.user.role.roleName == "教师") {
					index.isTcher = false;
					index.titlename = index.user.teacher.tname;
				} else {
					index.titlename = index.user.stu.stuname;
				}
			}
		});
	},
	identity : function() {
		$.ajax({
			type : "POST",
			url : "login_getcurrentuser",
			data : {},
			success : function(result) {
				var data = jQuery.parseJSON(result);
				index.user = data.item;
				$("#infoModal").modal('show');
				index.name = index.user.name;
				index.pwd = index.user.password;
				index.sex = index.user.sex;
				index.role = index.user.role.roleName;
			}
		});
	},
	updateUser : function() {
		$.ajax({
			type : "POST",
			url : "login_updateUser",
			data : {
				password : index.pwd,
				sex : index.sex
			},
			success : function(result) {
				$("#infoModal").modal('hide');
				swal(jQuery.parseJSON(result).msg, "", "success");
			}
		});
	},
	outlogin : function() {
		$.ajax({
			type : "POST",
			url : "login_outlogin",
			data : {},
			success : function(result) {
				window.location.href = "login_index";
			}
		});
	}
});
index.load();

// 加载学生列表
var StuManage = avalon.define({
	$id : "StuManage",
	dataList : {},
	id : "",
	Stu : "",
	name : "",
	stuno : "",
	banji : "",
	sex : "",
	age : "",
	major : "",

	load : function() {
		$.ajax({
			type : "POST",
			url : "stu_loadStulist",
			data : {},
			success : function(result) {
				StuManage.dataList = jQuery.parseJSON(result).items;
			}
		});
	},

	find : function(id) {
		$.ajax({
			type : "POST",
			url : "stu_findStu",
			data : {
				id : id
			},
			success : function(result) {
				StuManage.Stu = jQuery.parseJSON(result).item;
				$("#myModal").modal('show');
				StuManage.name = StuManage.Stu.stuname;
				StuManage.stuno = StuManage.Stu.stuid;
				StuManage.sex = StuManage.Stu.sex;
				StuManage.banji = StuManage.Stu.banji;
				StuManage.age = StuManage.Stu.age;
				StuManage.major = StuManage.Stu.major;
			}
		});
	},
	// edit : function(id) {
	// StuManage.isupdate = true;
	// StuManage.id = id;
	// StuManage.find(id);
	//	},
	update : function() {
		$.ajax({
			type : "POST",
			url : "stu_updateStu",
			data : {
				id : StuManage.id,
				sex : StuManage.sex,
				banji : StuManage.banji,
				age : StuManage.age,
				major : StuManage.major
			},
			success : function(result) {
				$("#myModal").modal('hide')
				swal(jQuery.parseJSON(result).msg, "", "success");
				StuManage.load();
			}
		});
	}

});

// 加载教师列表
var TcherManage = avalon.define({
	$id : "TcherManage",
	msg : "",
	dataList : {},
	name : "",
	no : "",
	sex : "",
	age : "",
	academic : "",
	college : "",
	tcher : "",

	load : function() {
		$.ajax({
			type : "POST",
			url : "tcher_loadTcherlist",
			data : {},
			success : function(result) {
				TcherManage.dataList = jQuery.parseJSON(result).items;
			}
		});
	},

	find : function(id) {
		$.ajax({
			type : "POST",
			url : "tcher_findTcher",
			data : {
				id : id
			},
			success : function(result) {
				TcherManage.tcher = jQuery.parseJSON(result).item;
				$("#myModal").modal('show');
				TcherManage.name = TcherManage.tcher.tname;
				TcherManage.no = TcherManage.tcher.tid;
				TcherManage.sex = TcherManage.tcher.sex;
				TcherManage.age = TcherManage.tcher.age;
				TcherManage.academic = TcherManage.tcher.academic;
				TcherManage.college = TcherManage.tcher.college;
			}
		});
	},
	// edit : function(id) {
	// TcherManage.id = id;
	// TcherManage.find(id);
	// },
	update : function() {
		$.ajax({
			type : "POST",
			url : "tcher_updatetcher",
			data : {
				tid : TcherManage.no,
				msg : TcherManage.sex,
				banji : TcherManage.age,
				cname : TcherManage.name,
				academic:TcherManage.academic,
				college:TcherManage.college
			},
			success : function(result) {
				$("#myModal").modal('hide')
				swal(jQuery.parseJSON(result).msg, "", "success");
				TcherManage.load();
			}
		});
	}
});

// 管理员查看教师列表
var adminsee = avalon.define({
	$id : "adminsee",
	itemList : {},
	adminSee : function() {
		$.ajax({
			type : "POST",
			url : "Appraise_loadAdminseeList",
			data : {},
			success : function(result) {
				adminsee.itemList = jQuery.parseJSON(result).items;
			}
		});
	},
	open : function(id, cname, banji) {
		$.ajax({
			type : "POST",
			url : "tcher_loadTableByTcherId",
			data : {
				tid : id,
				cname : cname,
				banji : banji
			},
			success : function(result) {
				AppraiseList.id = id;
				AppraiseList.cname = cname;
				AppraiseList.banji = banji;
				index.url = jQuery.parseJSON(result).url;
				AppraiseList.dataList = jQuery.parseJSON(result).items;
				AppraiseList.isManager = true;
				if (AppraiseList.dataList == "")
					AppraiseList.isNull = "当前没有任何学生进行评价";
			}
		});
	},
});

// 教师查看评价列表控制器
var AppraiseList = avalon.define({
	$id : "AppraiseList",
	isStu : false,
	isTcher : false,
	isManager : false,
	isNull : "已显示全部",
	itemList : {},
	dataList : {},
	selected : "",
	data : {},
	content : "",
	id : "",
	replystr : "",
	banji : "",
	cname : "",

	load : function() {
		$.ajax({
			type : "POST",
			url : "Appraise_loadAppraiseList",
			data : {},
			success : function(result) {
				AppraiseList.dataList = jQuery.parseJSON(result).items;
				if (jQuery.parseJSON(result).msg == "学生") {
					AppraiseList.isStu = true;
					if (AppraiseList.dataList == null) {
						AppraiseList.isNull = "当前没有评价任何老师";
					}
				}
			}
		});
	},
	// 老师查看自己所教班级评价列表
	TcherClassList : function(major, banji, cname) {
		$.ajax({
			type : "POST",
			url : "Appraise_loadTcherClassList",
			data : {
				major : major,
				banji : banji,
				cname : cname
			},
			success : function(result) {
				index.url = jQuery.parseJSON(result).url;
				AppraiseList.dataList = jQuery.parseJSON(result).items;
				AppraiseList.isTcher = true;
				if (AppraiseList.dataList == null) {
					AppraiseList.isNull = "当前没有学生评价你";
				}
			}
		});
	},
	TcherClassList1 : function(id) {
		$.ajax({
			type : "POST",
			url : "Appraise_loadTcherClassList1",
			data : {
				id : id
			},
			success : function(result) {
				index.url = jQuery.parseJSON(result).url;
				AppraiseList.dataList = jQuery.parseJSON(result).items;
				AppraiseList.isTcher = true;
				if (AppraiseList.dataList == null) {
					AppraiseList.isNull = "当前没有学生评价你";
				}
			}
		});
	},
	// 
	open : function(id, cname, banji) {
		$.ajax({
			type : "POST",
			url : "tcher_loadTableByTcherId",
			data : {
				tid : id,
				cname : cname,
				banji : banji
			},
			success : function(result) {
				// AppraiseList.id = id;
				// AppraiseList.cname = cname;
				index.url = jQuery.parseJSON(result).url;
				AppraiseList.dataList = jQuery.parseJSON(result).items;
				AppraiseList.isManager = true;
				if (AppraiseList.dataList == "")
					AppraiseList.isNull = "当前没有任何学生进行评价";
			}
		});
	},
	backTcherSee : function() {
		AppraiseList.TcherSeeList();
		index.url = "ClassList";
	},
	backadminSee : function() {
		index.url = "AdminTcherlistPage";
		AppraiseList.adminSee();
	},
	backStuAppraiselist : function() {
		index.url = "AppraiseList";
		AppraiseList.load();
	},
	// 返回列表方法
	returnpage : function() {
		index.url = "AppraiseList";
		AppraiseList.load();
	},
	// 教师查看每个班级的评价，一级菜单
	TcherSeeList : function() {
		$.ajax({
			type : "POST",
			url : "tcher_loadTcherSeeList",
			data : {},
			success : function(result) {
				AppraiseList.dataList = jQuery.parseJSON(result).items;
			}
		});
	},
	// 教师回复
	reply : function(id) {
		$.ajax({
			type : "POST",
			url : "Appraise_saveReply",
			data : {
				id : id,
				reply : AppraiseList.replystr
			},
			success : function(result) {
				swal(jQuery.parseJSON(result).msg, "", "success");
				AppraiseList.TcherClassList1(id);
				AppraiseList.replystr = "";
				$("#myModal").modal("hide");
			}
		});
	},

	// 教师查看详细评价，但无法修改
	TcherSee : function(id) {
		$.ajax({
			type : "POST",
			url : "Appraise_findAppraise",
			data : {
				id : id
			},
			success : function(result) {
				AppraiseList.id = id;
				AppraiseList.data = jQuery.parseJSON(result).item;
				var obj = eval(AppraiseList.data.selection).selectJson;
				AppraiseList.itemList = jQuery.parseJSON(obj);
				index.url = "appraiseSee";
				if (AppraiseList.data.score < 60
						&& AppraiseList.isManager == false) {
					swal({
						title : "提示",
						text : "学生对你的印象不太好，可以反思一下~.",
						timer : 5000
					});
				}
			}
		});
	},

	// 查找返回单个评教表对象
	edit : function(id) {
		$.ajax({
			type : "POST",
			url : "Appraise_findAppraise",
			data : {
				id : id
			},
			success : function(result) {
				AppraiseList.id = id;
				AppraiseList.data = jQuery.parseJSON(result).item;
				var obj = eval(AppraiseList.data.selection).selectJson;
				AppraiseList.itemList = jQuery.parseJSON(obj);
				index.url = "appraiseEdit";
			}
		});
	},

	// 保存修改
	save : function() {
		var select = $("select");
		var arr = new Array();
		for (var i = 0; i < select.length; i++) {
			arr[i] = select[i].value;
		}
		// 计算出总成绩后通过ajax把评价和成绩传回后台
		if ($("#pingjia").val() != "") {
			$.ajax({
				type : "POST",
				url : "appraise_edit",
				data : {
					id : AppraiseList.id,
					selectArr : arr.toString(),
					content : $("#pingjia").val()
				},
				success : function(result) {
					swal("修改成功", "修改后的评分为：" + jQuery.parseJSON(result).msg,
							"success");
					index.url = jQuery.parseJSON(result).url;
					AppraiseList.load();
				}
			});
		} else {
			swal("评价不写了吗~", "", "error");
		}
	}
});

// 评价页面控制器
var AppraisetcherManage = avalon.define({
	$id : "AppraisetcherManage",
	dataList : {},
	content : "",
	isOpen : false,
	isNull : "已显示全部",
	data : {
		tid : "",
		sid : ""
	},
	item : "",

	load : function() {
		$.ajax({
			type : "POST",
			url : "appraise_loadAppraiseList",
			data : {},
			success : function(result) {
				AppraisetcherManage.dataList = jQuery.parseJSON(result).items;
				if (jQuery.parseJSON(result).success == false) {
					swal({
						title : "通知",
						text : "管理员还没有开启评教，请过一段时间再来~.",
						timer : 5000
					});
				}
				AppraisetcherManage.isOpen = jQuery.parseJSON(result).success;
				if (AppraisetcherManage.dataList == "")
					AppraisetcherManage.isNull = "你已完成所有评价";
			}
		});
	},

	open : function(id) {
		$.ajax({
			type : "POST",
			url : "appraise_openappraisetable",
			data : {
				id : id
			},
			success : function(result) {
				AppraisetcherManage.id = id;
				AppraisetcherManage.item = jQuery.parseJSON(result);
				index.url = AppraisetcherManage.item.url;
				AppraisetcherManage.dataList = AppraisetcherManage.item.items;
				AppraisetcherManage.content = "";
				AppraisetcherManage.score = "";
			}
		});

	},
	save : function() {
		var select = $("select");
		var arr = new Array();
		for (var i = 0; i < select.length; i++) {
			arr[i] = select[i].value;
		}
		if ($("#pingjia").val() != "") {
			$.ajax({
				type : "POST",
				url : "appraise_save",
				data : {
					id : AppraisetcherManage.id,
					selectArr : arr.toString(),
					content : AppraisetcherManage.content
				},
				success : function(result) {
					swal("评价成功！", "评分为：" + jQuery.parseJSON(result).msg,
							"success");
					index.url = jQuery.parseJSON(result).url;
					AppraisetcherManage.load();
				}
			});
		} else {
			swal("写上几行评价吧~", "", "error");
		}

	}

});

// 管理评分项目控制器
var ScoreProj = avalon.define({
	$id : "ScoreProj",
	text : "",
	id : "",
	isopenscale : false,
	dataList : {},
	state : "",
	percent : "0% ",
	item : {
		content : "",
		scale : ""
	},
	openscale : function() {
		ScoreProj.isopenscale = true;
	},
	reback : function() {
		ScoreProj.isopenscale = false;
	},
	load : function() {
		$.ajax({
			type : "POST",
			url : "score_loadItemlist",
			data : {},
			success : function(result) {
				ScoreProj.dataList = jQuery.parseJSON(result).items;
				if (jQuery.parseJSON(result).success == true) {
					ScoreProj.state = "开启";
				}
				if (jQuery.parseJSON(result).success == false) {
					ScoreProj.state = "关闭";
				}
			}
		});
	},
	savepercent : function() {
		var input = $("input[name='percent']");
		var sum = 0;
		var arr = new Array();
		for (var i = 0; i < input.length; i++) {
			arr[i] = input[i].value;
			sum = parseInt(input[i].value) + sum;
		}
		if (sum != 100) {
			if (isNaN(sum)) {
				swal("请输入数字比例", "输入错误", "error");
			} else
				swal("当前总比例为 " + sum + " %", "所有比例之和应为100%", "error");
		} else {
			$.ajax({
				type : "POST",
				url : "score_saveItempercent",
				data : {
					percentArr : arr.toString(),
				},
				success : function(result) {
					ScoreProj.isopenscale = false;
					ScoreProj.load();
					swal("修改指标成功!", "", "success");
				}
			});
		}
	},
	open : function() {
		$.ajax({
			type : "POST",
			url : "score_openaddItem",
			data : {},
			success : function(result) {
				index.url = jQuery.parseJSON(result).url;
			}
		});
	},
	// 切换评教状态方法
	toggle : function(str) {
		$.ajax({
			type : "POST",
			url : "score_toggle",
			data : {
				state : str
			},
			success : function(result) {
				swal(jQuery.parseJSON(result).msg, "", "success");
				ScoreProj.load();
			}
		});
	},
	add : function() {
		if ($("#icontent").val() == "") {
			swal("请完整填写信息！");
			return;
		}
		$.ajax({
			type : "POST",
			url : "score_addItem",
			data : {
				content : ScoreProj.item.content,
				scale : ScoreProj.item.scale
			},
			success : function(result) {
				swal(jQuery.parseJSON(result).msg, "You clicked the button!",
						"success");
				$('input').val("");
				$('select').val("");
			}
		});

	},
	edit : function(id, content) {
		ScoreProj.text = content;
		ScoreProj.id = id;
	},

	update : function(id, content) {
		if (content != "") {
			$.ajax({
				type : "POST",
				url : "score_updateItem",
				data : {
					content : content,
					id : id
				},
				success : function(result) {
					$('#myModal').modal('hide');
					ScoreProj.load();
					swal(jQuery.parseJSON(result).msg, "", "success");
				}
			});
		} else {
			swal("内容不能为空~", "不能保存~", "error");
		}
	},

	del : function(id) {
		$('#myModal').modal('hide');
		swal({
			title : "您确定要删除吗？",
			text : "您确定要删除这条数据？",
			type : "warning",
			showCancelButton : true,
			closeOnConfirm : false,
			confirmButtonText : "是的，我要删除",
			confirmButtonColor : "#ec6c62"
		}, function() {
			$.ajax({
				url : "score_delItem",
				type : "POST",
				data : {
					id : id,
				},
			}).done(function(result) {
				swal("操作成功!", "已成功删除数据！", "success");
				ScoreProj.load();
			}).error(function(result) {
				swal("OMG", "删除操作失败了!", "error");
			});
		});
	}
});
